<template>
  <div>
    <div class="bgimg" :style="{backgroundImage: `url(${information.userBg})`}">
      <van-uploader class="bgbox" :after-read="changeBg"/>
    </div>
    <div class="content">
      <div class="my">
        <img :src="information.userImg" />
        <div>
          <h2>{{ information.nickName }}</h2>
          <p>
            {{
              information.desc ? information.desc : "这家伙很懒，什么都没有输入"
            }}
          </p>
        </div>
      </div>
      <div class="list">
        <van-cell is-link title="个人资料" to="Information" />
        <van-cell is-link title="我的订单" to="Myorder" />
        <van-cell is-link title="我的收藏" to="Like" />
        <van-cell is-link title="地址管理" to="Address" />
        <van-cell is-link title="安全中心" to="Security"/>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "My",
  data() {
    return {
      show: false,
      actions: [{ name: "选项一" }, { name: "选项二" }, { name: "选项三" }],
      information: [],
    };
  },
  methods: {
    // 获取用户信息
    getInformation() {
      let token = this.$cookies.get("TOKEN");

      this.axios({
        method: "GET",
        url: "/findMy",
        params: {
          tokenString: token,
        },
      })
        .then((res) => {
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast("请先登录");
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 2000);
            return;
          }
          this.information = res.data.result[0];
        })
        .catch((err) => {
          
        });
    },
    // 修改背景图
    changeBg(file){
      let base64 = file.content.replace(/^data:image\/[A-Za-z]+;base64,/, '');

      let tokenString = this.$cookies.get("TOKEN");

      this.axios({
        method: "POST",
        url: "/updateUserBg",
        data: {
          tokenString,
          serverBase64Img: base64,
          imgType: file.file.type.split(/\//)[1]
        },
      })
        .then((res) => {

          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast("请先登录");
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 2000);

            return;
          }

          this.$toast(res.data.msg);

          if (res.data.code === 'I001' && res.data.result[0] === 1) {
            this.information.userBg = res.data.userBg;
          }
         

          
        })
        .catch((err) => {
          
        });
    },
  },
  created() {
    this.getInformation();
  },
};
</script>

<style lang="less" scoped>
.bgimg {
  position: relative;
  width: 100%;
  height: 160px;
  background-color: pink;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  .bgbox {
    width: 100%;
    height: 100%;
    opacity: 0;
  }
  /deep/ .van-uploader__wrapper {
    width: 100%;
    height: 100%;
  }
  /deep/ .van-uploader__upload {
    width: 100%;
    height: 100%;
    margin: 0;
  }
}
.content {
  margin: 0 15px;
  position: relative;
  background-color: rgba(255, 255, 255, 0.7);
  top: -50px;
  border-top-right-radius: 15px;
  border-top-left-radius: 15px;
  .my {
    display: flex;
    margin-bottom: 20px;
    padding: 10px;
    img {
      width: 70px;
      height: 70px;
      border-radius: 50%;
      margin-right: 15px;
    }
    h2 {
      padding-top: 0;
      // margin-bottom: 15px;
    }
  }
}
</style>